package com.example.config;


import com.example.action.SmsCommitAction;
import com.example.action.SmsSendSuccessAction;
import com.example.enums.Events;
import com.example.enums.Status;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import java.util.EnumSet;

@Configuration
@EnableStateMachine
public class SmsStateMachineConfig extends StateMachineConfigurerAdapter<Status, Events> {

    /***
     *
     * 初始化所有状态
     * @param states
     * @throws Exception
     */
    @Override
    public void configure(StateMachineStateConfigurer<Status, Events> states) throws Exception {
        states.withStates().initial(Status.READY)
                .states(EnumSet.allOf(Status.class));
    }

    /***
     * 定义监听和事件
     * @param transitions
     * @throws Exception
     */
    @Override
    public void configure(StateMachineTransitionConfigurer<Status, Events> transitions) throws Exception {
        transitions.withExternal()
                .source(Status.READY).target(Status.SUCCESS)
                .event(Events.SUCCESSED)
                .action(new SmsSendSuccessAction())
                .and()
                .withExternal()
                .source(Status.SUCCESS).target(Status.COMMIT)
                .event(Events.COMMITED)
                .action(new SmsCommitAction());
    }
}